/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;
import javax.swing.Timer;
import model.Auktion;
import model.Maleri;
import model.Møbel;
import model.Smykke;
import model.Vin;
import utility.ImageToolbox;
import utility.Procent;

/**
 *
 * @author Jeppe
 */
public class AuktionslistePanel extends javax.swing.JPanel {

    private MainGui main;
    private Auktion aktivAuktion;
    private ImageToolbox imgTB;
    private Timer t;

    /**
     * Creates new form AuktionslistePanel
     */
    public AuktionslistePanel(MainGui main, Auktion auktion) {
        initComponents();
        this.main = main;
        this.aktivAuktion = auktion;
        setFields(auktion);
        jProgressBar1.setStringPainted(true);
        jProgressBar1.setForeground(new Color(91, 231, 255));
        jProgressBar1.setBackground(Color.WHITE);

        // laver milisekunder om til antal timer siden 1.1.1970
        long start = TimeUnit.MILLISECONDS.toHours(auktion.getStartTidspunkt().getTimeInMillis());
        long slut = TimeUnit.MILLISECONDS.toHours(auktion.getSluttidspunkt().getTimeInMillis());
        long nuvaerende = TimeUnit.MILLISECONDS.toHours(System.currentTimeMillis());

        // procent udregning
        double timeProcentSats = Procent.procentAfInterval(start, slut, nuvaerende);

        // updater progressbar med den nye procentsats
        jProgressBar1.setValue((int) Math.round(timeProcentSats));

        // set billede
        billedeLabel.setIcon(auktion.getVareIcon());
        jProgressBar1.setForeground(Color.BLACK);
        calculateTimeLeft();
        // Vin vin = (Vin)main.getAuktionsHandler().getAuktioner().get(1).getVare();
        // ekstraTxtArea.setText(vin.getAuktionslisteData());
        t = new Timer(10000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                calculateTimeLeft();
            }
        });
        t.start();
    }

    public void calculateTimeLeft() {
        Calendar first = Calendar.getInstance();
        Calendar second = aktivAuktion.getSluttidspunkt();

        long diff = second.getTimeInMillis() - first.getTimeInMillis();

        if (diff <= 0) {
            jProgressBar1.setVisible(false);
            jLabel1.setText("Auktionen er udløbet");
            jLabel1.setForeground(Color.red);
        } else {
            long minutesLeft = diff / (60 * 1000) % 60;

            long hoursLeft = diff / (60 * 60 * 1000) % 24;

            long daysLeft = diff / (24 * 60 * 60 * 1000);

            jProgressBar1.setString(daysLeft + " dage, " + hoursLeft + " timer, " + minutesLeft + " minutter");

        }
    }

    private void setFields(Auktion auktion) {
        SimpleDateFormat simpel = new SimpleDateFormat();
        simpel.format(auktion.getSluttidspunkt().getTime());
        sluttidLabel.setText(simpel.format(auktion.getSluttidspunkt().getTime()));
        vurderingsprisLabel.setText("Vurderingspris: " + auktion.getVare().getVurderingspris() + " kr.");
        senestebudLabel.setText("Seneste bud: " + Math.round(auktion.getSenesteBud().getBeløb()) + " kr.");
        switch (auktion.getVare().getKategori().getNavn()) {
            case "Møbel":
                Møbel møbel;
                møbel = (Møbel) auktion.getVare();
                varetitelLabel.setText(møbel.getNavn());
                // slutDatoLabel.setText(auktion.getSluttidspunkt().getTime(). "/"+auktion.getSluttidspunkt().getMonth()+1+" "+auktion.getSluttidspunkt().getYear());
                // sluttidLabel.setText(auktion.getSluttidspunkt().getHours()+ ":" + auktion.getSluttidspunkt().getMinutes() +"");
                break;
            case "Vin":
                Vin vin;
                vin = (Vin) auktion.getVare();
                varetitelLabel.setText(vin.getNavn());
                ekstraTxtArea.setText(vin.getAuktionslisteData());
                break;
            case "Maleri":
                Maleri maleri;
                maleri = (Maleri) auktion.getVare();
                varetitelLabel.setText(maleri.getNavn());
                ekstraTxtArea.setText(maleri.getAuktionsListeData());
                break;
            case "Smykke":
                Smykke smykke;
                smykke = (Smykke) auktion.getVare();
                varetitelLabel.setText(smykke.getNavn());
                break;
            default:
                varetitelLabel.setText("Fejl i varen");

        }
        ekstraTxtArea.repaint();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        billedeLabel = new javax.swing.JLabel(){
            public void paint (Graphics g){
                super.paint(g);
                imgTB.drawImage(g, aktivAuktion.getVareIcon(), 0, 0, billedeLabel.getSize().width, billedeLabel.getSize().height);
            }
        }
        ;
        varetitelLabel = new javax.swing.JLabel();
        vurderingsprisLabel = new javax.swing.JLabel();
        sluttidLabel = new javax.swing.JLabel();
        jProgressBar1 = new javax.swing.JProgressBar();
        senestebudLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ekstraTxtArea = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setPreferredSize(new java.awt.Dimension(589, 120));
        setLayout(null);

        billedeLabel.setText("billede");
        billedeLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        add(billedeLabel);
        billedeLabel.setBounds(2, 2, 116, 116);

        varetitelLabel.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        varetitelLabel.setText("Varetitel");
        add(varetitelLabel);
        varetitelLabel.setBounds(130, 10, 200, 17);

        vurderingsprisLabel.setText("Vurderingspris");
        add(vurderingsprisLabel);
        vurderingsprisLabel.setBounds(130, 40, 160, 14);

        sluttidLabel.setText("Sluttid");
        add(sluttidLabel);
        sluttidLabel.setBounds(470, 100, 100, 14);

        jProgressBar1.setBackground(new java.awt.Color(51, 51, 51));
        jProgressBar1.setForeground(new java.awt.Color(51, 51, 255));
        jProgressBar1.setToolTipText("");
        jProgressBar1.setValue(70);
        jProgressBar1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jProgressBar1.setStringPainted(true);
        add(jProgressBar1);
        jProgressBar1.setBounds(240, 100, 200, 19);

        senestebudLabel.setText("Seneste bud");
        add(senestebudLabel);
        senestebudLabel.setBounds(130, 60, 160, 14);

        jScrollPane1.setBorder(null);

        ekstraTxtArea.setEditable(false);
        ekstraTxtArea.setColumns(20);
        ekstraTxtArea.setRows(3);
        ekstraTxtArea.setBorder(null);
        ekstraTxtArea.setEnabled(false);
        jScrollPane1.setViewportView(ekstraTxtArea);

        add(jScrollPane1);
        jScrollPane1.setBounds(330, 10, 250, 80);

        jButton1.setBorderPainted(false);
        jButton1.setContentAreaFilled(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        add(jButton1);
        jButton1.setBounds(0, 0, 590, 120);

        jLabel1.setText("jLabel1");
        add(jLabel1);
        jLabel1.setBounds(240, 100, 200, 20);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        main.showVisAuktionPanel(aktivAuktion);
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel billedeLabel;
    private javax.swing.JTextArea ekstraTxtArea;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel senestebudLabel;
    private javax.swing.JLabel sluttidLabel;
    private javax.swing.JLabel varetitelLabel;
    private javax.swing.JLabel vurderingsprisLabel;
    // End of variables declaration//GEN-END:variables
}
